import urllib2
import MySQLdb
import config
import datetime


def get_data(url):
    try:
        data = urllib2.urlopen(url).read()
        return data
    except:
        return ''

def get_result_sql(conn,cmd):
    try:
        conn.ping()
    except:
        conn = create_conn()
    cursor = conn.cursor(MySQLdb.cursors.DictCursor)
    cursor.execute(cmd)
    result = cursor.fetchall()
    cursor.close()
    return result


def get_date_today():
    now = datetime.datetime.now()
    return now.strftime("%Y-%m-%d")


def get_datetime_today():
    now = datetime.datetime.now()
    return now.strftime("%Y-%m-%d %H-%M")



def create_conn():
    conn = MySQLdb.connect (host=config.mysql_local,
                            user=config.mysql_user,
                            passwd=config.mysql_pass,
                            db=config.mysql_db,
                            port=config.mysql_port,
                            charset='utf8',
                            use_unicode=False,
                            )
    conn.autocommit(True)
    return conn
    